package com.xiaosheng.ljswl.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.xiaosheng.ljswl.Config.SerialApplication;
import com.xiaosheng.ljswl.R;
import com.xiaosheng.ljswl.model.Node.*;

import node.utils.RouterUtil;
import node.utils.Sensor;
import serial.utils.SerialZbThread;


public class MultFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private RouterUtil routerUtil;
    private byte type;
    private TextView titleMult;
    private TextView titleMac;
    private TextView MacAdd;
    private RadioGroup radioGroup;
    private RadioButton Dcf;
    private RadioButton FengShan;
    private RadioButton BuGuangDeng;
    private Button open;
    private Button clear;
    private TextView titleSmg;
    private EditText Smg;
    private Button Show;
    private TextView titleDj;
    private Button ZhuanLeft;
    private Button ZhuanRight;
    private Button Stop;
    private Button CF;

    public MultFragment() {
    }

    public static RelayFragment newInstance(String param1, String param2) {
        RelayFragment fragment = new RelayFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mult, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        findView(view);
        //创建继电器工具类
        routerUtil = new RouterUtil();
        SerialZbThread serialZbThread = ((SerialApplication) getActivity().getApplication()).getSerialZbThread();
        serialZbThread.setiZgibeeRec((buffer, len) -> {
            Router router;
            if ((router = Sensor.getRouter(buffer, len)) != null) {
                if (router.type == 0x09) {
                    routerUtil.setMacaddr(router.macaddr);
                    routerUtil.setAddr(router.addr);
                }
                final Router finalRouter = router;
                getActivity().runOnUiThread(() -> MacAdd.setText(finalRouter.mac));
            } else {
                serialZbThread.resetComDataHandler();
            }
        });
        CF.setOnClickListener(v -> serialZbThread.sendZigBee(Sensor.CFNode((byte) 0x09)));
        open.setOnClickListener((v) -> serialZbThread.sendZigBee(routerUtil.MultDcfOpen(type)));
        clear.setOnClickListener((v) -> serialZbThread.sendZigBee(routerUtil.MultDcfClear(type)));
        radioGroup.setOnCheckedChangeListener((radioGroup, i) -> {
            switch (i) {
                case R.id.Dcf:
                    type = RouterUtil.DCF;
                    break;
                case R.id.FengShan:
                    type = RouterUtil.FS;
                    break;
                case R.id.BuGuangDeng:
                    type = RouterUtil.BGD;
                    break;
            }
        });
        ZhuanLeft.setOnClickListener(v -> serialZbThread.sendZigBee(routerUtil.MultDj((byte) 0xAB, (byte) 0x05)));
        ZhuanRight.setOnClickListener(v -> serialZbThread.sendZigBee(routerUtil.MultDj((byte) 0xBA, (byte) 0x05)));
        Stop.setOnClickListener(v -> serialZbThread.sendZigBee(routerUtil.MultDj((byte) 0x00, (byte) 0x05)));
        super.onViewCreated(view, savedInstanceState);
    }

    private void findView(View view) {
        titleMult = (TextView) view.findViewById(R.id.title_mult);
        titleMac = (TextView) view.findViewById(R.id.title_mac);
        MacAdd = (TextView) view.findViewById(R.id.MacAdd);
        radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
        Dcf = (RadioButton) view.findViewById(R.id.Dcf);
        FengShan = (RadioButton) view.findViewById(R.id.FengShan);
        BuGuangDeng = (RadioButton) view.findViewById(R.id.BuGuangDeng);
        open = (Button) view.findViewById(R.id.open);
        clear = (Button) view.findViewById(R.id.clear);
        titleSmg = (TextView) view.findViewById(R.id.title_smg);
        Smg = (EditText) view.findViewById(R.id.Smg);
        Show = (Button) view.findViewById(R.id.Show);
        titleDj = (TextView) view.findViewById(R.id.title_dj);
        ZhuanLeft = (Button) view.findViewById(R.id.ZhuanLeft);
        ZhuanRight = (Button) view.findViewById(R.id.ZhuanRight);
        Stop = (Button) view.findViewById(R.id.Stop);
        CF = (Button) view.findViewById(R.id.CF);
    }
}